package com.hp.interview.concurrent.threads_run_in_order;

import java.util.concurrent.Semaphore;

/**
 * @author libo
 * 使用Sephmore(信号量)实现线程按顺序运行
 */
public class SemaphoreDemo {

    private static Semaphore semaphore1 = new Semaphore(1);
    private static Semaphore semaphore2 = new Semaphore(1);



    public static void main(String[] args) {

        final Thread thread1 = new Thread(() -> {
            System.out.println("产品经理规划新需求");
            semaphore1.release();
        });

        final Thread thread2 = new Thread(() -> {
            try {
                semaphore1.acquire(2); //因为信号量不够,可能会阻塞（此中情况未尝试，请大家监督）
                System.out.println("开发人员开发新需求功能");
                semaphore2.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread thread3 = new Thread(() -> {
            try {
                semaphore2.acquire(2);
                System.out.println("测试人员测试新功能");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread3.start();
        thread1.start();
        thread2.start();
    }
}